package com.example.temp.mapper;

import com.example.temp.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface StudentMapper {
    // 插入学生
    @Insert("INSERT INTO student(studentID, name, gender, phone, notes, className, classId) VALUES (#{studentID}, #{name}, #{gender}, #{phone}, #{notes}, #{className}, #{classId})")
    int insert(Student student);

    // 修改学生信息
    @Update("UPDATE student SET name = #{name}, gender = #{gender}, phone = #{phone}, notes = #{notes}, className = #{className} WHERE studentID = #{studentID}")
    int update(Student student);

    // 查询分页
    @Select("SELECT * FROM student WHERE name LIKE CONCAT('%',#{params.query},'%') LIMIT #{params.pageNum}, #{params.pageSize}")
    List<Student> search(@Param("params") Map<String, Object> params);

    // 查询数据总数
    @Select("SELECT COUNT(*) FROM student WHERE name LIKE CONCAT('%',#{query},'%')")
    int getTotal(@Param("query") String query);

    // 根据 studentID 删除学生
    @Delete("DELETE FROM student WHERE studentID = #{studentID}")
    int deleteByID(@Param("studentID") String studentID);

    // 根据学生id查询
    @Select("select * from student where StudentID = #{studentID}")
    Student findByID(@Param("studentID") String studentID);

    // 批量添加
    int batchInsert(@Param("list") List<Student> students);
    
    // 根据班级名称查询学生
    @Select("SELECT * FROM student WHERE className = #{className}")
    List<Student> findByClassName(@Param("className") String className);
    
    // 根据班级ID查询学生
    @Select("SELECT * FROM student WHERE classId = #{classId}")
    List<Student> findByClassId(@Param("classId") Integer classId);
    
    // 查找未分配班级的学生（className为NULL或空字符串，或classId为NULL）
    @Select("SELECT * FROM student WHERE classId IS NULL")
    List<Student> findStudentsWithNoClass();
    
    // 更新学生班级（使用班级ID）
    @Update("UPDATE student SET classId = #{classId} WHERE studentID = #{studentID}")
    int updateClassInfo(@Param("studentID") String studentID, @Param("className") String className, @Param("classId") Integer classId);
    
    // 保留此方法签名以兼容代码，实际上将classId设置为Null
    @Update("UPDATE student SET classId = NULL WHERE studentID = #{studentID}")
    int updateClassName(@Param("studentID") String studentID, @Param("className") String className);
    
    // 根据学号前缀查询学生（用于自动完成）
    @Select("SELECT * FROM student WHERE studentID LIKE CONCAT(#{prefix},'%') LIMIT 10")
    List<Student> findByStudentIdPrefix(@Param("prefix") String prefix);
}
